package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreAdvFictitious;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreFictitious;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreUser;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreAdvFictitiousMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreFictitiousMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreUserMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.business.wallet.mapper.WalletMapper;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.constant.ConstantNumber;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * WalletResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-22 15:24:00
 */
@Slf4j
@Component
public class WalletResolveMethod implements ResolveMethod {

    @Resource
    private WalletMapper walletMapper;
    @Resource
    private OldCoreFictitiousMapper oldCoreFictitiousMapper;
    @Resource
    private OldCoreAdvFictitiousMapper oldCoreAdvFictitiousMapper;

    @Resource
    private OldCoreUserMapper oldCoreUserMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 查询余额信息
        var wallet = walletMapper.selectById(trigger.getTargetId());
        if (wallet == null) {
            throw new BusinessSimpleException("未查询到钱包信息");
        }
        var balance = new BigDecimal(wallet.getBalance()).divide(ConstantNumber.BIG_DECIMAL_HUNDRED, 2, RoundingMode.HALF_UP).doubleValue();
        // 设置余额信息
        var wrapper = new LambdaQueryWrapper<OldCoreFictitious>()
                .eq(OldCoreFictitious::getBcXuniUserId, wallet.getFkUserId())
                .orderByDesc(OldCoreFictitious::getBcXuniTime)
                .last("limit 1");
        var oldCoreFictitious = oldCoreFictitiousMapper.selectOne(wrapper);
        if (oldCoreFictitious == null) {
            // 新增
            oldCoreFictitious = new OldCoreFictitious();
            oldCoreFictitious.setBcXuniUserId(wallet.getFkUserId());
            oldCoreFictitious.setBcXuniMoney(balance);
            oldCoreFictitious.setBcXuniTime(wallet.getGmtModified());
            oldCoreFictitiousMapper.insert(oldCoreFictitious);
        } else {
            var wrapper2 = new LambdaUpdateWrapper<OldCoreFictitious>()
                    .eq(OldCoreFictitious::getBcXuniUserId, wallet.getFkUserId())
                    .set(OldCoreFictitious::getBcXuniMoney, balance);
            oldCoreFictitiousMapper.update(wrapper2);
        }
        // 设置广告钱包
        oldCoreAdvFictitiousMapper.update(new LambdaUpdateWrapper<OldCoreAdvFictitious>()
                .eq(OldCoreAdvFictitious::getUserId, wallet.getFkUserId())
                .set(OldCoreAdvFictitious::getFictitiousTotal, 0)
        );
        // 设置对应的钱包密码
        if (StringUtils.isNotBlank(wallet.getPassword())) {
            var userUpdateWrapper = new LambdaUpdateWrapper<OldCoreUser>()
                    .eq(OldCoreUser::getUserId, wallet.getFkUserId())
                    .set(OldCoreUser::getUserPayPwd, wallet.getPassword());
            oldCoreUserMapper.update(userUpdateWrapper);
        }
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "wallet";
    }

}
